package ru.usu.gv.gui.clustering;

import ru.usu.gv.graph.Graph;

/**
 * @author spupyrev
 * 17.11.2008
 */
public class BinaryGraph
{
	int n;
	int totalWeight;

	int[][] links;
	int[][] weights;

	public BinaryGraph()
	{
	}

	public BinaryGraph(Graph graph)
	{
		n = graph.getSize();
		links = new int[n][];
		weights = new int[n][];
		for (int i = 0; i < graph.getSize(); i++)
		{
			links[i] = new int[graph.getVertex(i).getEdgesSize()];
			weights[i] = new int[graph.getVertex(i).getEdgesSize()];
			for (int j = 0; j < graph.getVertex(i).getEdgesSize(); j++)
			{
				links[i][j] = graph.getVertex(i).getEdge(j).getIndex();
				weights[i][j] = 1;
				totalWeight += weights[i][j];
			}
		}
	}

	// return the number of self loops of the node
	public int countSelfloops(int node)
	{
		assert (node >= 0 && node < n);

		for (int i = 0; i < links[node].length; i++)
		{
			if (links[node][i] == node)
				return weights[node][i];
		}
		return 0;
	}

	// return the weighted degree of the node
	public int getWeightedDegree(int node)
	{
		assert (node >= 0 && node < n);

		int res = 0;
		for (int i = 0; i < weights[node].length; i++)
			res += weights[node][i];
		return res;
	}

	public int getDegree(int from, int to)
	{
		for (int i = 0; i < links[from].length; i++)
			if (links[from][i] == to)
				return weights[from][i];

		return 0;
	}

	public void display()
	{
		for (int node = 0; node < n; node++)
		{
			for (int i = 0; i < links[node].length; i++)
			{
				System.out.println("" + node + " " + links[node][i] + " " + weights[node][i]);
			}
		}
	}
}
